/**
 * 滑动窗口，按官解看，是求连续子数组且子数组种类不超过三种
 * @param {number[]} fruits
 * @return {number}
 */
var totalFruit = function (fruits) {
    let length = fruits.length,
        res = 0,
        left = 0,
        curFruitMap = new Map()

    for (let right = 0; right < length; right++) {
        curFruitMap.set(fruits[right], (curFruitMap.get(fruits[right]) || 0) + 1)
        while (curFruitMap.size > 2) {
            curFruitMap.set(fruits[left], curFruitMap.get(fruits[left]) - 1)
            if (curFruitMap.get(fruits[left]) === 0) {
                curFruitMap.delete(fruits[left])
            }
            left++
        }
        res = res >= (right - left + 1) ? res : (right - left + 1)
    }

    return res
};

console.log(totalFruit([0, 1, 2, 2]))